package org.example.controller;

import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.ClassAccount;
import org.example.pojo.Result;
import org.example.service.ClassAccountService;
import org.example.service.PermissionService;
import org.example.utils.CheckUtils;
import org.example.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/classAccounts")
public class ClassAccountController {
    @Autowired
    private ClassAccountService classAccountService;
    @Autowired
    private PermissionService permissionService;

    @PostMapping(consumes = "application/json")
    public Result addClassAccount(@RequestHeader String token,
                                  @RequestBody ClassAccount classAccount) {
        Claims claims = JWTUtils.parseToken(token);
        if(!permissionService.permissionInChangeClassAccountTable(claims)) {
            log.error("User {} has no permission to add class account", claims.get("name"));
            return Result.error("User has no permission to add class account");
        }
        classAccountService.addClassAccount(classAccount);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteClassAccountById(@RequestHeader String token,
                                         @PathVariable int id) {
        Claims claims = JWTUtils.parseToken(token);
        if(!permissionService.permissionInChangeClassAccountTable(claims)) {
            log.error("User {} has no permission to delete class account", claims.get("name"));
            return Result.error("User has no permission to delete class account");
        }
        classAccountService.deleteClassAccountById(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result updateClassAccountById(@RequestHeader String token,
                                         @PathVariable int id,
                                         @RequestBody ClassAccount classAccount) {
        Claims claims = JWTUtils.parseToken(token);
        if(!permissionService.permissionInChangeClassAccountTable(claims)) {
            log.error("User {} has no permission to update class account", claims.get("name"));
            return Result.error("User has no permission to update class account");
        }
        classAccountService.updateClassAccountById(classAccount, id);
        return Result.success();
    }

    @GetMapping("/{classCode}")
    public Result queryClassAccountByClassId(@RequestHeader String token,
                                             @PathVariable String classCode) {
        Claims claims = JWTUtils.parseToken(token);
        if(!permissionService.permissionInQueryClassAccountTable(claims, classCode)) {
            log.error("User {} has no permission to query class account", claims.get("name"));
            return Result.error("User has no permission to query class account");
        }
        List<ClassAccount> classAccounts = classAccountService.queryClassAccountByClassId(classCode);
        return Result.success(classAccounts);
    }
}
